/*
 * Term.java
 *
 * Created on July 17, 2007, 2:35 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.atomojo.app.client;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;
import org.infoset.xml.Element;
import org.infoset.xml.Name;

/**
 *
 * @author alex
 */
public class Term implements Comparable
{
   static Logger LOG = Logger.getLogger(Term.class.getName());
   public static final URI DEFAULT_SCHEME = URI.create("http://www.atomojo.org/O/keyword/");

   URI term;
   List<String> values;
   
   /** Creates a new instance of Term */
   public Term(URI term)
   {
      this.term = term;
      this.values = null;
   }
   
   /** Creates a new instance of Term */
   public Term(URI term,String value)
   {
      this.term = term;
      this.values = value!=null ? Collections.singletonList(value) : null;
   }
   
   public URI getURI() {
      return term;
   }
   
   public List<String> getValues() {
      return values;
   }
   
   public String getFirstValue() {
      return values!=null ? values.get(0) : null;
   }
   
   public void add(String value)
   {
      if (values==null) {
         values = Collections.singletonList(value);
      } else if (values instanceof ArrayList) {
         values.add(value);
      } else {
         List<String> old = values;
         values = new ArrayList<String>();
         values.addAll(old);
         values.add(value);
      }
   }
   
   public void remove(String value)
   {
      if (values==null) {
         return;
      }
      if (values.size()==1 && values.contains(value)) {
         values = null;
         return;
      }
      values.remove(value);
   }
   
   public URI getScheme() {
      return term.resolve(".");
   }
   
   public String getName() {
      URI parent = getScheme();
      return parent.relativize(term).toString();
   }
   
   public static String getValue(Element category) {
      
      String value = category.getText();
      if (value!=null) {
         value = value.trim();
         if (value.length()==0) {
            value = null;
         }
      }
      if (value==null) {
         value = category.getAttributeValue("value");
      }
      return value;
   }
   
   public static Term derive(Element category)
   {
      return derive(DEFAULT_SCHEME,category);
   }
   
   public static Term derive(URI defaultScheme,Element category)
   {
      String scheme = category.getAttributeValue("scheme");
      String term = category.getAttributeValue("term");
      if (term!=null) {
         term = term.trim();
         if (term.length()==0) {
            term = null;
         } else {
            try {
               term = URLEncoder.encode(term,"UTF-8");
            } catch (UnsupportedEncodingException ex) {
               LOG.severe("Cannot encode term due to: "+ex.getMessage());
               return null;
            }
         }
      }
      if (term==null) {
         return null;
      }
      String value = getValue(category);
      if (scheme!=null) {
         scheme = scheme.trim();
         if (scheme.length()==0) {
            scheme =null;
         }
      }
      URI uscheme = null;
      if (scheme!=null) {
         uscheme = URI.create(scheme);
      } else {
         if (defaultScheme==null) {
            throw new IllegalArgumentException("Default scheme is null and term "+term+" has no scheme.");
         }
         uscheme = defaultScheme;
      }
      URI uterm = uscheme.resolve(term).normalize();
      return new Term(uterm,value);
   }

   public int compareTo(Object o) {
      if (o instanceof Term) {
         return term.compareTo(((Term)o).getURI());
      } else {
         return -1;
      }
   }
   
}
